<?php
/**
 * Zend
 * 
 * @category	Loader
 * @package		dayscript.loader.adapter
 * @subpackage	
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license		
 * @version		1.1
 * @version		$Revision: 0 $
 * @filesource	
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		
 * @since		1.0
 * @modifiedby	$LastChangedBy: Juan Carlos Orrego $
 * @modified	$Date: 2009-12-01 $
 */

namespace dayscript\loader\adapter;

/**
 * Zend
 * 
 * Library auto loader for Zend framework
 * Be warned that Zend makes internal requiere(...)/include(...) calls and this call will not be logged.
 * 
 * @category	Loader
 * @package		dayscript.loader.adapter
 * @subpackage	
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license		
 * @version		1.0
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		\dayscript\loader\Loader
 * @since		1.0
 * @modifiedby	$LastChangedBy: Nelson Daza $
 * @modified	$Date: 2009-09-01 $
 */
class Zend extends \dayscript\loader\Loader	{
	/**
	 * Zend constructor
	 * 
	 * It will add the $libraryDir to actual includedir
	 * 
	 * @param string $name Loader identifier
	 * @param string $libraryDir Directory where the libraries are placed, with a trailing DIRECTORY_SEPARATOR.
	 * @return Zend
	 */
	public function __construct( $name, $libraryDir )	{
		parent::__construct( $name, $libraryDir, '.php' );
		$includeDirs = \explode( \PATH_SEPARATOR, \get_include_path( ) );

		// This $key value is used to put system share path, after
		// user supplied path. Usefull for not using system Zend
		// installation. 
		if( $key = \array_search("/usr/share/php",$includeDirs) )
			\array_splice($includeDirs,$key,1);
			
		if( !\in_array( $libraryDir, $includeDirs ) )	{
			$includeDirs[] = $libraryDir;
			if($key)$includeDirs[] = "/usr/share/php";
			\set_include_path( \implode( \PATH_SEPARATOR, $includeDirs ) );
		}
	}
	/**
	 * {@inheritdoc }
	 */
	public function canLoadClass( $className )	{
		return ( \strpos( $className, 'Zend_' ) === 0  && \file_exists( $this->libraryDir . \str_replace( '_', \DIRECTORY_SEPARATOR, $className ) . $this->extension ) );
	}
	/**
	 * {@inheritdoc }
	 */
	public function loadClass( $className )	{
		if( \strpos( $className, 'Zend_' ) === 0 )
			require( $this->libraryDir . \str_replace( '_', \DIRECTORY_SEPARATOR, $className ) . $this->extension );
	}
}